﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.BLL
{
    public partial class SellersOrganizationService
    { /// <summary>
        /// 一级机构
        /// </summary>
        /// <returns></returns>
        public IQueryable<SellersOrganization> GetSellersOrganizationAll()
        {
            return LoadEntities(o => o.IsAvailable);
        }
        public bool IsExist(string name, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<SellersOrganization>();
            if (!string.IsNullOrEmpty(name))
            {
                expressionWhere = expressionWhere.And(o => o.OrganizationName.Equals(name));
            }
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }
        public IQueryable<SellersOrganization> GetPagingList(SellersOrganizationModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SellersOrganization>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => o.OrganizationName.ToLower().Contains(kw));
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "Sort asc";
            }
            return LoadPagerEntities<SellersOrganization>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        public SellersOrganization LoadByID(long id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        public SellersOrganization LoadParentOrganization(long parentID)
        {
            return LoadEntities(a => a.ID == parentID).FirstOrDefault();
        }

        public IQueryable<SellersOrganization> GetSellersOrganizationAllByParentId(long parentId)
        {
            if (parentId > 0)
            {
                return LoadEntities(a => a.ParentOrganizationId == parentId);
            }
            else
            {
                return LoadEntities(a => a.ParentOrganizationId > 0);
            }
        }
    }
}
